package cn.edu.hbvtc.qms.persist.hibernate;

import java.util.List;

import cn.edu.hbvtc.qms.domain.ClazzArrangement;
import cn.edu.hbvtc.qms.domain.EvalPeriod;
import cn.edu.hbvtc.qms.domain.Evaluation;
import cn.edu.hbvtc.qms.domain.Term;
import cn.edu.hbvtc.qms.domain.User;
import cn.edu.hbvtc.qms.persist.IEvaluationDao;

public class HibernateEvaluationDao extends
		HibernateAbstractDao<Evaluation, Integer> implements IEvaluationDao {

	private static final long serialVersionUID = 1L;

	public HibernateEvaluationDao() {
		super(Evaluation.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Evaluation> findEvaluationByEvaluated(User evaluated) {
		return this.getHibernateTemplate()
				.find("from Evaluation e where e.evaluated.id = ?",
						evaluated.getId());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Evaluation> findEvaluationByEvaluated(User evaluated, Term term) {
		return this.getHibernateTemplate().find(
				"from Evaluation e where e.evaluated.id = ? and e.term.id = ?",	new Object[]{evaluated.getId(), term.getId()});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Evaluation> findEvaluationByClazzArrangementAndEvalPeriod(ClazzArrangement ca, EvalPeriod ep) {
		return this.getHibernateTemplate().find("from Evaluation e where e.clazz.id = ? and e.course.id = ? and e.evaluated.id = ? and e.term.id = ? and e.evalPeriod.id = ?",
				new Object[] {ca.getClazz().getId(), ca.getCourse().getId(), ca.getTeacher().getId(), ca.getTerm().getId(), ep.getId()});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Evaluation findEvaluationByEvaluator(User evaluator,	ClazzArrangement ca, EvalPeriod ep) {
		List<Evaluation> evalList = this.getHibernateTemplate().find("from Evaluation e where e.evaluator.id = ? and e.clazz.id = ? and e.course.id = ? and e.evaluated.id = ? and e.evalPeriod.id = ?",
				new Object[] {evaluator.getId(), ca.getClazz().getId(), ca.getCourse().getId(), ca.getTeacher().getId(), ep.getId()});
		Evaluation evaluation = null;
		if (evalList != null && evalList.size() > 0) {
			evaluation = evalList.get(0);
		}
		return evaluation;
	}

}
